<template>
  <a-modal
    :title="title"
    :width="1400"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['title', {rules: [{required: true, min: 2, message: '请输入至少2个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label="封面图" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <SelectImage
            :defaultList="record.image ? [record.image] : []"
            v-decorator="['image_id', { rules: [{ required: true, message: '请选择1个封面图' }] }]"
          />
        </a-form-item>
        <a-form-item
              label="轮播图片"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              extra="建议尺寸：750*750像素, 最多上传10张, 可拖拽图片调整顺序"
            >
              <SelectImage
                multiple
                :maxNum="10"
                :defaultList="record.carousel_images"
                v-decorator="['imagesIds', { rules: [{ required: true, message: '请至少上传1张轮播图片' }] }]"
              />
            </a-form-item>
          
        <a-form-item label="推荐商品" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <template>
            <div>
              <a-select
                mode="multiple"
                :size="size"
                placeholder="请选择推荐商品"
                v-decorator="['goods_ids', { rules: [{required: true, message: '请选择1个推荐商品'}]}]"
                style="width: 400px"
                @change="handleChange"
                @popupScroll="popupScroll"
              >
              <a-select-option
                v-for="(item, index) in goodsList"
                :key="index"
                :value="item.goods_id"
              >{{ item.goods_id }}、{{ item.goods_name }}</a-select-option>

                <!-- <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
                  {{ (i + 9).toString(36) + i }}
                </a-select-option> -->
              </a-select>
            </div>
          </template>

        </a-form-item>
        <a-form-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <Ueditor v-decorator="['describe', { rules: [{ required: true, message: '详情内容不能为空' }] }]" />
        </a-form-item>
      
        <a-form-item label="锁定状态" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用户端是否锁定状态">
          <a-radio-group v-decorator="['lock_status', {initialValue: 0, rules: [{required: true}]}]">
            <a-radio :value="1">锁定</a-radio>
            <a-radio :value="0">解锁</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">
          <a-input-number
            :min="0"
            v-decorator="['sort', {initialValue: 100, rules: [{required: true, message: '请输入至少1个数字'}]}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as RecommendApi from '@/api/goods/recommend'
import { SelectImage, Ueditor } from '@/components'

export default {
  components: {
    SelectImage,
    Ueditor
  },
  props: {
   
  },
  data () {
    return {
      size: 'default',
      // 对话框标题
      title: '编辑商品推荐文章',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),

      // 当前记录ID
      recommendId: null,
      // 当前记录详情
      record: {},
      goodsList: [{}],
      // 正在加载
      isLoading: false,
    }
  },
  created () {
    // 获取商品列表
    this.getGoodsList()
  },
  methods: {
    /**
     * 获取分类列表
     */
     getGoodsList () {
      // console.log(111);
      this.isLoading = true
      RecommendApi.Goodslist({})
        .then(result => {
          // console.log(result.data.newlist)
          this.goodsList = result.data.newlist
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    handleChange(text) {
      console.log(`Selected: ${text}`);
    },
    popupScroll() {
      console.log('popupScroll');
    },
    /**
     * 显示对话框
     */
    edit (recommendId) {
      // 显示窗口
      this.visible = true
      // 当前记录ID
      this.recommendId = recommendId
      // 获取当前详情记录
      this.getDetail()
    },

    // 获取当前详情记录
    getDetail () {
      this.confirmLoading = true
      RecommendApi.detail({ recommendId: this.recommendId })
        .then(result => {
          // 当前记录
          this.record = result.data.detail
          // 设置默认值
          this.setFieldsValue()
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(this.record, [
          'title', 'describe',  'image_id',  'sort', 'lock_status','goods_ids'
        ]))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.record = {}
      this.form.resetFields()
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      RecommendApi.edit({ recommendId: this.recommendId, form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
